package com.songshuai.webapi.crm.service.impl;

import com.songshuai.webapi.baseapi.dto.UserUpdatePwdDto;
import com.songshuai.webapi.baseapi.dto.feign.request.UserAddReqDto;
import com.songshuai.webapi.baseapi.dto.feign.request.UserInfoUpdateReqDto;
import com.songshuai.webapi.baseapi.enumerations.ResultStatus;
import com.songshuai.webapi.baseapi.pojo.BaseParmDto;
import com.songshuai.webapi.baseapi.pojo.QueryPageRes;
import com.songshuai.webapi.baseapi.pojo.QueryResponse;
import com.songshuai.webapi.baseapi.dto.feign.response.AccountInfoResDto;
import com.songshuai.webapi.baseapi.dto.feign.response.AccountPageLIstResDto;
import com.songshuai.webapi.baseapi.utils.EncryptUtil;
import com.songshuai.webapi.baseapi.utils.RequestParamValidUtil;
import com.songshuai.webapi.crm.constant.CrmConstant;
import com.songshuai.webapi.crm.dao.CustomInfoMapper;
import com.songshuai.webapi.crm.dto.request.account.AccountPageListReqDto;
import com.songshuai.webapi.crm.service.AccountManagerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestBody;

import javax.validation.Valid;
import java.util.List;

/**
 * @Author: lhx
 * @Date: 2018/12/28 10:23
 */
@Service
public class AccountManagerServiceImpl extends BaseServiceImpl implements AccountManagerService {





    @Autowired
    private CustomInfoMapper customInfoMapper;


    /**
     * 分页模糊查询用户信息
     * @param request
     * @return
     */
    @Override
    public QueryResponse<QueryPageRes<List<AccountPageLIstResDto>>> queryAccountListByPage(BaseParmDto<AccountPageListReqDto> request) {
        QueryResponse<QueryPageRes<List<AccountPageLIstResDto>>> response = permissionClient.queryAccountListByPage(getBearerToken(),request.getParam().getAccount(), request.getParam().getStatus(), request.getPageIndex(), request.getPageSize());
        if (null == response){
            return new QueryResponse(ResultStatus.FAIL);
        }
        return response;
    }

    /**
     * 新增用户
     * @param request
     * @return
     */
    @Override
    public QueryResponse addUser(UserAddReqDto request) {
        QueryResponse response = permissionClient.addUser(getBearerToken(),request);
        if (null == response){
            return new QueryResponse(ResultStatus.FAIL);
        }
        return response;
    }

    /**
     * 根据用户id查询用户信息
     * @param id
     * @return
     */
    @Override
    public QueryResponse<AccountInfoResDto> queryUserInfoById(Integer id) {
        QueryResponse<AccountInfoResDto> response = permissionClient.queryUserInfoById(getBearerToken(),id);
        if (null == response){
            return new QueryResponse(ResultStatus.FAIL);
        }
        return response;
    }

    /**
     * 修改用户信息
     * @param requset
     * @return
     */
    @Override
    public QueryResponse updateUserById(UserInfoUpdateReqDto requset) {
        QueryResponse response = permissionClient.updateUserById(getBearerToken(),requset);
        if (null == response){
            return new QueryResponse(ResultStatus.FAIL);
        }
        return response;
    }


    /**
     * 禁用/启用
     * @param id
     * @return
     */
    @Override
    public QueryResponse updateUserStatusById(Integer id, Integer status) {
        QueryResponse response = permissionClient.updateUserStatusById(getBearerToken(),id, status);
        if (null == response){
            return new QueryResponse(ResultStatus.FAIL);
        }
        if ( CrmConstant.USER_STATUS_DISABLED.equals(status) && ResultStatus.SUCCESS.getErrorCode()==response.getCode()){
            forbiddenLoginUser(String.valueOf(id));//禁用用户，强制退出
        }
        return response;
    }


    /**
     * 根据用户id修改密码
     * @param id
     * @param pwd
     * @return
     */
    @Override
    public QueryResponse updateUserPwdById(Integer id, String pwd) {
        QueryResponse response = permissionClient.updateUserPwdById(getBearerToken(),id, pwd);
        if (null == response){
            return new QueryResponse(ResultStatus.FAIL);
        }
        return response;
    }

    /**
     * 查询账户下跟进学员人数
     * @param id
     * @return
     */
    @Override
    public Integer queryCustomNum(Integer id) {
        return customInfoMapper.selectNumByOwner(id);
    }

    /**
     * 删除用户
     * @param id
     * @return
     */
    @Override
    public QueryResponse deleteUserById(Integer id) {

        //判断账号是否仍有跟进学员
        Integer num = customInfoMapper.selectNumByOwner(id);
        if (num>0){
            return new QueryResponse(ResultStatus.ERROR_DELETE_FAILED.getErrorCode(),"删除失败，该账户下仍有跟进的学员"+num+"名");
        }
        QueryResponse response = permissionClient.deleteUserById(getBearerToken(),id);
        if (null == response){
            return new QueryResponse(ResultStatus.FAIL);
        }
        return response;
    }

    @Override
    public QueryResponse updateUserPassword(UserUpdatePwdDto userUpdatePwdDto) {
        QueryResponse response = permissionClient.updateUserPassword(getBearerToken(),userUpdatePwdDto);
        return response;
    }


}
